package ar.com.itcollege.vista;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import ar.com.itcollege.BD.UsuariosDB;
import ar.com.itcollege.ConectionMysql;
import ar.com.itcollege.modelo.Usuario;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author educacionit
 */
public class VistaAlumnos extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ObtengoAlumnos</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Los alumnos que tengo son </h1>");
            
            ArrayList<Usuario> usuariosDB = UsuariosDB.getUsuariosDB();
            String [] encabezadoUsuarios = {"ID","NOMBRE","CONTRASEÑA","ES ADMIN"};
            out.println(getTablaDeAlumnos(usuariosDB, encabezadoUsuarios));
           /* int cantidad = 0;
            for (Iterator<Usuario> it = usuariosDB.iterator(); it.hasNext();) {
                Usuario usuario = it.next();
                
                cantidad ++;
                
                out.println("<b>Usuario " + cantidad +"</b><br/>");
                out.println(" ID: "+usuario.getId() +"<br/>");
                out.println(" Usuario: "+usuario.getUsuario() +"<br/>");
                out.println(" Contraseña: "+usuario.getContrasena() +"<br/>");
                out.println(" EsAdmin?: "+usuario.getAdmin() +"<br/>");
                      
                
            }*/
           
            
            
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    /**
     * Retorna una tabla con los alumnos
     * @param losAlumnos
     * @param encabezado
     * @return 
     */
    public static String getTablaDeAlumnos (ArrayList<Usuario> losUsuarios, String[] encabezado){
        String tablaDeAlumnos;
        
        tablaDeAlumnos = "<table border=1>";
        
        //Pongo los títulos
        tablaDeAlumnos += "<tr >";
        for (int i = 0; i < encabezado.length; i++) {
            tablaDeAlumnos += "<td>"+encabezado[i]+"</td>";
        }
        tablaDeAlumnos += "</tr>";
        
        
        //Pongo los alumnos
        for (Iterator<Usuario> it = losUsuarios.iterator(); it.hasNext();) {
            Usuario usuario = it.next();
            tablaDeAlumnos += "<tr><td>"+usuario.getId()
                    +"</td><td>"+usuario.getUsuario()
                    +"</td><td>"+usuario.getContrasena()
                    +"</td><td>"+usuario.getAdmin()
                    +"</td></tr>";
        }
                            
        tablaDeAlumnos += "</table>";
                            
        return tablaDeAlumnos;
    }
}
